package com.coolbi.onsupai.admin.action;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.ImageUtil;
import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.GoodsCategoryBiz;
import com.coolbi.onsupai.core.biz.GoodsInfoBiz;
import com.coolbi.onsupai.core.biz.MerchantBiz;
import com.coolbi.onsupai.core.constant.GoodsConstant;
import com.coolbi.onsupai.core.entity.GoodsCategory;
import com.coolbi.onsupai.core.entity.GoodsInfo;
import com.coolbi.onsupai.core.entity.Merchant;
import com.coolbi.security.web.action.SessionAction;

/**
 * @Description 商品信息Action
 * @author for2cold
 * @codeDate 2012-6-23 上午02:12:19
 *
 */
public class GoodsAction extends SessionAction {

	private static final long serialVersionUID = 1L;
	/** 文件上传目的文件夹 */
	private static final String UPLOAD_FLODER = "upload";
	private GoodsInfo goodsInfo;
	private List<GoodsInfo> goodsList;
	@Autowired
	private GoodsInfoBiz goodsInfoBiz;
	private String keyWord;
	private int searchType;
	private List<GoodsCategory> goodsCategoryList;
	@Autowired
	private GoodsCategoryBiz goodsCategoryBiz;
	private Integer orderNo;
	private File upload;
	private List<Merchant> merchantList;
	@Autowired
	private MerchantBiz merchantBiz;
	private String goodsIds;
	
	/**
	 * 添加商品
	 * @return
	 * @throws Exception
	 */
	public String save() {
		try {
			goodsInfo.setPictureUrl(ImageUtil.copyImageFile(getServletContext(), upload, goodsInfo.getGoodsNo(), UPLOAD_FLODER));
			goodsInfoBiz.save(goodsInfo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 分页查看所有商品
	 * @return
	 * @throws Exception
	 */
	public String listByPage() throws Exception {
		try {
			pager.setRecordCount(goodsInfoBiz.count());
			goodsList = goodsInfoBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
			Collections.sort(goodsList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 根据商品类别查找商品
	 * @return
	 * @throws Exception
	 */
	public String listByCategory() throws Exception {
		searchType = GoodsConstant.GOODS_CATEGORY;
		pager.setRecordCount(goodsInfoBiz.countByKeyWord(searchType, keyWord));
		goodsList = goodsInfoBiz.queryByKeyWord(searchType, keyWord, 
					(pager.getCurrPageNo() - 1) * pager.getPageSize(), 
					pager.getPageSize());
		Collections.sort(goodsList);
		return SUCCESS;
	}
	
	/**
	 * 根据关键字查询
	 * @return
	 * @throws Exception
	 */
	public String findByKeyWord() throws Exception {
		pager.setRecordCount(goodsInfoBiz.countByKeyWord(searchType, keyWord));
		goodsList = goodsInfoBiz.queryByKeyWord(searchType, keyWord, 
					(pager.getCurrPageNo() - 1) * pager.getPageSize(), 
					pager.getPageSize());
		Collections.sort(goodsList);
		return SUCCESS;
	}
	
	/**
	 * 获取所有商品种类、供应商
	 * @return
	 * @throws Exception
	 */
	public String findAllCategory() throws Exception {
		goodsCategoryList = goodsCategoryBiz.list();
		merchantList = merchantBiz.list();
		return SUCCESS;
	}
	
	/**
	 * 根据商品ID查询
	 * @return
	 * @throws Exception
	 */
	public String findByGoodsId() throws Exception {
		goodsInfo = goodsInfoBiz.get(goodsInfo.getId());
		goodsCategoryList = goodsCategoryBiz.list();
		return SUCCESS;
	}
	
	/**
	 * 根据商品ID集合查询商品，返回Json格式数据
	 * 
	 * @return
	 */
	public String findByIds() {
		String json = null;
		try {
			goodsList = goodsInfoBiz.findByIds(goodsIds);
			json = JsonUtil.toJsonByArray(goodsList);
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
		return ajax(json);
	}
	
	/**
	 * 检测商品编号是否已存在
	 * @return
	 */
	public String checkGoodsNo() {
		try {
			boolean flag = false;
			if (null == goodsInfo.getId()) {
				flag = goodsInfoBiz.checkGoodsNo(goodsInfo.getGoodsNo());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 更新商品信息
	 * @return
	 */
	public String update() {
		try {
			goodsInfoBiz.update(goodsInfo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 根据商品ID获取商品信息，并返回Json数据
	 * @return
	 */
	public String findGoodsById() {
		try {
			goodsInfo = goodsInfoBiz.get(goodsInfo.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		String json = JsonUtil.toJsonByObject(goodsInfo);
		return ajax(json);
	}
	
	public GoodsInfo getGoodsInfo() {
		return goodsInfo;
	}
	public void setGoodsInfo(GoodsInfo goodsInfo) {
		this.goodsInfo = goodsInfo;
	}
	public void setGoodsInfoBiz(GoodsInfoBiz goodsInfoBiz) {
		this.goodsInfoBiz = goodsInfoBiz;
	}

	public List<GoodsInfo> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<GoodsInfo> goodsList) {
		this.goodsList = goodsList;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public int getSearchType() {
		return searchType;
	}

	public void setSearchType(int searchType) {
		this.searchType = searchType;
	}

	public List<GoodsCategory> getGoodsCategoryList() {
		return goodsCategoryList;
	}

	public void setGoodsCategoryList(List<GoodsCategory> goodsCategoryList) {
		this.goodsCategoryList = goodsCategoryList;
	}

	public void setGoodsCategoryBiz(GoodsCategoryBiz goodsCategoryBiz) {
		this.goodsCategoryBiz = goodsCategoryBiz;
	}

	public Integer getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(Integer orderNo) {
		this.orderNo = orderNo;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public List<Merchant> getMerchantList() {
		return merchantList;
	}

	public void setMerchantList(List<Merchant> merchantList) {
		this.merchantList = merchantList;
	}

	public void setMerchantBiz(MerchantBiz merchantBiz) {
		this.merchantBiz = merchantBiz;
	}

	public String getGoodsIds() {
		return goodsIds;
	}

	public void setGoodsIds(String goodsIds) {
		this.goodsIds = goodsIds;
	}
}
